package com.sxhuayuan.parking.plugin.parking.xianyun;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.sxhuayuan.parking.compenent.Message;
import com.sxhuayuan.parking.compenent.web.RespMessage;
import com.sxhuayuan.parking.config.Platform;
import com.sxhuayuan.parking.dto.CreateMcardChargeOrderDTO;
import com.sxhuayuan.parking.dto.ParkFeeCacheBean;
import com.sxhuayuan.parking.entity.Member;
import com.sxhuayuan.parking.entity.MemberCarPlate;
import com.sxhuayuan.parking.entity.ParkingIn;
import com.sxhuayuan.parking.entity.ParkingLot;
import com.sxhuayuan.parking.exception.MyException;
import com.sxhuayuan.parking.plugin.parking.ParkingPlugin;
import com.sxhuayuan.parking.plugin.parking.result.McardCancelResultWrap;
import com.sxhuayuan.parking.plugin.parking.result.McardChargeOrderCreateResultWrap;
import com.sxhuayuan.parking.plugin.parking.result.McardQueryChargeOptionsResultWrap;
import com.sxhuayuan.parking.plugin.parking.result.McardQueryResultWrap;
import com.sxhuayuan.parking.plugin.parking.result.ParkingLotInfoQueryResultWrap;
import com.sxhuayuan.parking.plugin.parking.result.QueryParkingFeeResultWrap;
import com.sxhuayuan.parking.queue.msg.NotifyParkingLotMsg;
import com.sxhuayuan.parking.service.ExitingCarService;
import com.sxhuayuan.parking.service.OrderService;
import com.sxhuayuan.parking.service.ParkChannelService;
import com.sxhuayuan.parking.service.ParkFeeCacheService;
import com.sxhuayuan.parking.service.ParkingInService;
import com.sxhuayuan.parking.service.ParkingLotService;
import com.sxhuayuan.parking.service.impl.pojo.ExitingCar;
import com.sxhuayuan.parking.utils.DateUtils;
import com.sxhuayuan.parking.utils.SettingUtils;

import lombok.extern.log4j.Log4j2;

/**
 * 
 * 咸运集团新华设备接口（车场主动调用模式）
 * 
 * @author gy
 */
@Log4j2
@Component(XianyunParkingPlugin.ID)
public class XianyunParkingPlugin extends ParkingPlugin {

	public static final String ID = "xianyunParkingPlugin";

	@Resource(name = "orderServiceImpl")
	OrderService orderService;
	@Autowired
	ParkingInService inService;
	@Autowired
	ParkChannelService channelService;
	@Autowired
	ParkingLotService lotService;
	@Autowired
	ExitingCarService exitingCarService;
	@Autowired
	ParkFeeCacheService parkFeeCacheService;

	@Override
	public String getId() {
		return ID;
	}

	@Override
	public boolean isSupportEventPush() {
		return true;
	}

	@Override
	public String getName() {
		return "咸运停车设备";
	}

	@Override
	public ParkFeeCacheBean saveParkInRecord(String lotSn, String carPlate, String resp) {
		ExitingCar exitingCar = JSON.parseObject(resp, ExitingCar.class);
		ParkFeeCacheBean bean = new ParkFeeCacheBean();
		bean.setCarPlate(exitingCar.getCarPlate());
		bean.setDiscountFee(0f);
		bean.setTotalFee(exitingCar.getFee());
		if (SettingUtils.get().getIsDebug()) {
			bean.setTotalFee(0.01f);
		}
		bean.setEndTime(DateUtils.parse(exitingCar.getOutTime()));
		bean.setExitDevice(exitingCar.getDevice());
		bean.setExitId(exitingCar.getExitid());
		bean.setLotOrderNo("");
		bean.setParkPluginId(ID);
		bean.setUniqueId(exitingCar.getUniqueId());
		return bean;
	}

	@Override
	public QueryParkingFeeResultWrap queryParkingFee(ParkingLot lot, String carPlate) {
		throw new MyException(RespMessage.Status.ERROR, Message.API_QUERY_FEE_FAIL);
	}

	@Override
	public QueryParkingFeeResultWrap queryLeavingCarParkingLotOrder(ParkingLot lot, String exitSn, Member member) {
		QueryParkingFeeResultWrap rs = null;
		ExitingCar exitingCar = exitingCarService.get(exitSn);
		String carPlate = null;
		if (exitingCar != null) {
			// throw new MyException(Message.API_QUERY_FEE_FAIL);
			carPlate = exitingCar.getCarPlate();
		}
		rs = new QueryParkingFeeResultWrap();
		if (StringUtils.isEmpty(carPlate)) {
			rs.setIsSuccess(false);
			rs.setMsg("未找到出口车牌");
			return rs;
		} else {
			rs.setCarPlate(carPlate);
			rs.setFee(exitingCar.getFee());
			rs.setCalcFee(exitingCar.getFee());
			rs.setEntertime(exitingCar.getInTime());
			rs.setCalcStartTime(exitingCar.getInTime());
			rs.setCalcEndTime(exitingCar.getOutTime());
			rs.setParktimeMinute(exitingCar.getParkMinutes());
			ParkingIn in = inService.findByCarPlate(lot.getId(), carPlate);
			rs.setUniqueId(String.valueOf(in.getUniqueId()));
			Float fee = exitingCar.getFee();
			submitUpdateOriginalInfoTask(lot.getSn(), carPlate, fee, JSON.toJSONString(exitingCar));
		}
		return rs;
	}

	@Override
	public boolean notifyParkingFeeOrderSuccess(NotifyParkingLotMsg notify) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean notifyMonthCardChargeOrderSuccess(String order, String lotSn, String carPlate) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public McardQueryResultWrap monthCardQuery(String lotSn, MemberCarPlate carPlate) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public McardQueryResultWrap monthCardChargePreview(String lotSn, String carPlate, Integer month) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ParkingLotInfoQueryResultWrap parkingLotInfoQuery(String lotSn) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public McardQueryChargeOptionsResultWrap monthCardQueryChargeOptions(String lotSn) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public McardChargeOrderCreateResultWrap monthCardCreateOrder(MemberCarPlate memberCarPlate, CreateMcardChargeOrderDTO dto, Platform platform) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public McardCancelResultWrap monthCardCancel(String lotSn, String carPlate) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String execute(String api, String lotSn, Map map) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void afterInNoticeHanler(HttpServletRequest request, HttpServletResponse response, ParkingLot parkingLot) {

	}

	@Override
	public void afterOutNoticeHanler(HttpServletRequest request, HttpServletResponse response, ParkingLot parkingLot) {

	}
	
	@Override
	public void beforeOutNoticeHandler(HttpServletRequest request, HttpServletResponse response, ParkingLot parkingLot) {
		// TODO Auto-generated method stub
		
	}

}
